export class GroupRule{

  rules = [];
  transrule = [];
  noderule = [];

  constructor(ruleMaps) {

    var selected = {name: "" , database: "" , table: ""};
    var value = "";
    var nullvalue = "";
    var nullname = "";
    var enmvalue = ""
    var regexvalue = "";
    var regexname = "";
    var grpname = "";

    for (let key in ruleMaps) {
      selected.name = key;
      selected.database = ruleMaps[key].database;
      selected.table = ruleMaps[key].table;
      let info = "";
      let otherinfo = "";
      for (let i = 0; i < ruleMaps[key]['value'].length; i++) {
        var originrule = ruleMaps[key]['value'][i];
        info = info + originrule + ",";

        if (originrule == "Enum Detection Top5 Count") {

          enmvalue = this.transferRule(originrule, selected);
          grpname = `${selected.name}_top5count`;
          this.pushEnmRule(enmvalue, grpname);

        } else if (originrule == "Null Count") {

          nullvalue = this.transferRule(originrule, selected);
          nullname = `${selected.name}_nullcount`;
          this.pushNullRule(nullvalue, nullname);

        } else if (originrule == "Empty Count") {

          nullvalue = this.transferRule(originrule, selected);
          nullname = `${selected.name}_emptycount`;
          this.pushNullRule(nullvalue, nullname);

        } else if (originrule == "Regular Expression Detection Count") {

          selected['regex'] = ruleMaps[key].regex;
          regexvalue = this.transferRule(originrule, selected);
          regexname = `${selected.name}_regexcount`;
          this.pushRegexRule(regexvalue, regexname);

        } else {

          otherinfo = otherinfo + originrule + ",";
          value = this.transferRule(originrule, selected);
          this.transrule.push({
            name: originrule,
            rule: value
          });

        }
      }

      info = info.substring(0, info.lastIndexOf(","));
      otherinfo = otherinfo.substring(0, otherinfo.lastIndexOf(","));
      this.noderule.push({
        name: key,
        infos: info
      });
    }
    if (this.transrule.length != 0) {
      this.getRule(this.transrule);
    }
  }

  getRule(transrule) {
    transrule.forEach(
      item => {
        this.pushRule(item);
      }
    )
  }

  pushEnmRule(rule, grpname) {
    this.rules.push({
      "dsl.type": "griffin-dsl",
      "dq.type": "PROFILING",
      rule: rule,
      "out.dataframe.name": grpname,
      "out": [
        {
          "type": "metric",
          "name": grpname,
          "flatten": "array"
        }
      ]
    });
  }

  pushNullRule(rule, nullname) {
    this.rules.push({
      "dsl.type": "griffin-dsl",
      "dq.type": "PROFILING",
      rule: rule,
      "out.dataframe.name": nullname
    });
  }

  pushRegexRule(rule, nullname) {
    this.rules.push({
      "dsl.type": "griffin-dsl",
      "dq.type": "PROFILING",
      rule: rule,
      "out.dataframe.name": nullname
    });
  }

  pushRule(rule) {
    this.rules.push({
      "dsl.type": "spark-sql",
      "dq.type": "PROFILING",
      rule: rule.rule,
      'out.dataframe.name': 'count_'+rule.name,
      "out": [
        {
          "type":"metric",
          "name":"count_emptyIs"
        },
        {
            "type":"record",
            "name":"count_emptyIs"
        }
      ]
    });
  }

  transferRule(rule, col) {
    console.log(rule, col);
    switch (rule) {
      case "Total Count":
        return (
          `count(source.${col.name}) AS \`${col.name}_count\``
        );
      case "Distinct Count":
        return (
          `approx_count_distinct(source.${col.name}) AS \`${col.name}_distcount\``
        );
      case "Null Count":
        return (
          `count(source.${col.name}) AS \`${col.name}_nullcount\` WHERE source.${col.name} IS NULL`
        );
      case "Maximum":
        return (
          `max(source.${col.name}) AS \`${col.name}_max\``
        );
      case "Minimum":
        return (
          `min(source.${col.name}) AS \`${col.name}_min\``
        );
      case "Average":
        return (
          `avg(source.${col.name}) AS \`${col.name}_average\``
        );
      case "Empty Count":
        return (
          `count(source.${col.name}) AS \`${col.name}_emptycount\` WHERE source.${col.name} = ''`
        );
      case "Regular Expression Detection Count":
        return (
          `count(source.${col.name}) AS \`${col.name}_regexcount\` WHERE source.${col.name} RLIKE '^[0-9]{4}$'`
        );
      case "Enum Detection Top5 Count":
        return (
          `source.${col.name} AS ${col.name}, count(*) AS count GROUP BY source.${col.name} ORDER BY count DESC LIMIT 5`
        );
      default:
        return (
          `select COUNT(*) AS count_${rule} from ${col.database}.${col.table} WHERE ${rule}(${col.name})`
        )
    }
  }
}